//
//  HRZAPIClient.m
//  EMarket
//
//  Created by 物联速通信息技术有限公司 on 2018/5/23.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZAPIClient.h"

// Controllers

// Models

// Views

// Vendors
#import "PPNetworkHelper.h"
// Categories

// Others

@interface HRZAPIClient ()

@end

@implementation HRZAPIClient

- (id)init {
    @throw [NSException exceptionWithName:NSInternalInconsistencyException
                                   reason:[NSString stringWithFormat:@"%@ Failed to call designated initializer.", NSStringFromClass([self class])]
                                 userInfo:nil];
}

+ (HRZAPIClient *)sharedInstance {
    static HRZAPIClient *_apiClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _apiClient = [[HRZAPIClient alloc] init];
    });
    return _apiClient;
}

+ (NSString *)baseURLString {
    return @"";
}

/** 发起不同请求 */
+ (void)execute:(HRZAPIRequest *)request {
    switch (request.requestType) {
        case kAPIRequestType_Get:
        {
            [HRZAPIClient executeGetRequestWithApi:request];
        }
            break;
        case kAPIRequestType_Post:
        {
            [HRZAPIClient executePostRequestWithApi:request];
        }
            break;
        case kAPIRequestType_Upload:
        {
            [HRZAPIClient executeUploadRequestWithApi:request];
        }
            break;
            
        default:
            break;
    }
}

/** 执行GET网络请求 */
+ (void)executeGetRequestWithApi:(HRZAPIRequest *)request
{
    [PPNetworkHelper GET:request.fullUrl parameters:request.params responseCache:^(id responseCache) {
        
        [request callBackFinishedWithDictionary:(NSDictionary*)responseCache];
    } success:^(id responseObject) {
        
        [request callBackFinishedWithDictionary:(NSDictionary*)responseObject];
        
    } failure:^(NSError *error) {
        
        [request callBackFailed:error];
        
    }];
}

/** 执行post网络请求 */
+ (void)executePostRequestWithApi:(HRZAPIRequest *)request
{
    [PPNetworkHelper POST:request.fullUrl parameters:request.params responseCache:^(id responseCache) {
        [request callBackFinishedWithDictionary:(NSDictionary*)responseCache];
    } success:^(id responseObject) {
        
        [request callBackFinishedWithDictionary:(NSDictionary *)responseObject];
        
    } failure:^(NSError *error) {
        
        [request callBackFailed:error];
        
    }];
}
//  上传图片
+ (void)executeUploadRequestWithApi:(HRZAPIRequest *)request
{
    [PPNetworkHelper uploadImagesWithURL:request.fullUrl parameters:request.params name:@"" images:request.images fileNames:@[@"file"] imageScale:1.0 imageType:@"png" progress:^(NSProgress *progress) {
        
    } success:^(id responseObject) {
        
        [request callBackFinishedWithDictionary:(NSDictionary *)responseObject];
        
    } failure:^(NSError *error) {
        
        [request callBackFailed:error];
        
    }];
    
}


@end
